package com.hyzx.qbasic.admin.domain.service.impl;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.dto.OrgTreeInfo;
import com.hyzx.qbasic.common.util.TreeNode;
import com.hyzx.qbasic.admin.model.po.OrgInfoPO;
import com.hyzx.qbasic.admin.model.po.OrgPO;
import com.hyzx.qbasic.admin.model.vo.req.admin.AddOrgReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.DeleteOrgReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.EditOrgReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.GetOrgListReq;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.common.util.OperationResUtils;
import com.hyzx.qbasic.common.util.TreeNodeUtils;
import com.hyzx.qbasic.admin.domain.repository.IOrgRepository;
import com.hyzx.qbasic.admin.domain.service.IOrgService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 黄江华 on 2017/11/14
 */

@Service
public class OrgServiceImpl implements IOrgService {
    @Autowired
    private IOrgRepository orgRepository;

    @Override
    public PageInfo<OrgInfoPO> getPagedOrgList(GetOrgListReq req) {
        PageInfo<OrgInfoPO> orgList = orgRepository.getPagedOrgList(
                req.getPageNum(), req.getPageSize(), req.getOrgName(), req.getOrgId());

        return orgList;
    }

    @Override
    public BaseOperationRes addOrg(AddOrgReq req) {
        OrgPO orgPO = new OrgPO();
        orgPO.setOrgName(req.getOrgName());
        orgPO.setOrgCode(req.getOrgCode());
        orgPO.setPriority(req.getPriority());
        orgPO.setParentId(req.getParentIdList().get(req.getParentIdList().size() - 1));
        orgPO.setCreateTime(new Date());
        orgPO.setUpdateTime(new Date());

        boolean success = orgRepository.addOrg(orgPO);
        if (!success)
            return OperationResUtils.fail("新增组织机构失败");

        return OperationResUtils.success();
    }

    @Override
    public List<OrgTreeInfo> getOrgTreeList() {
        List<OrgPO> poList = orgRepository.getAllOrgList();
        if (CollectionUtils.isEmpty(poList))
            return Collections.emptyList();

        List<OrgTreeInfo> treeInfoList = new ArrayList<>(poList.size());
        poList.forEach(po -> {
            OrgTreeInfo treeInfo = new OrgTreeInfo();
            BeanUtils.copyProperties(po, treeInfo);

            treeInfoList.add(treeInfo);
        });

        return treeInfoList;
    }

    @Override
    public BaseOperationRes deleteOrg(DeleteOrgReq req) {
        List<OrgPO> orgList = orgRepository.getAllOrgList();
        List<TreeNode> treeNodeList = orgList.stream()
                .map(orgInfo -> new TreeNode(orgInfo.getOrgId(), orgInfo.getOrgName(), orgInfo.getParentId()))
                .collect(Collectors.toList());

        List<Integer> childList = new ArrayList<>(orgList.size());

        for (Integer orgId : req.getOrgIdList()) {
            childList.addAll(TreeNodeUtils.getChildNodes(treeNodeList, orgId));
        }

        boolean success = orgRepository.deleteOrg(childList.stream().distinct().collect(Collectors.toList()));
        if (!success)
            return OperationResUtils.fail("删除组织机构失败");

        return OperationResUtils.success();
    }

    @Override
    public OrgPO getOrgInfo(int orgId) {
        return orgRepository.getOrgInfo(orgId);
    }

    @Override
    public BaseOperationRes editOrg(EditOrgReq req) {
        //检查组织机构是否存在
        OrgPO existedOrg = orgRepository.getOrgInfo(req.getOrgId());
        if (existedOrg == null)
            return OperationResUtils.fail("组织机构不存在");

        OrgPO orgPO = new OrgPO();
        orgPO.setOrgId(req.getOrgId());
        orgPO.setOrgName(req.getOrgName());
        orgPO.setOrgCode(req.getOrgCode());
        orgPO.setPriority(req.getPriority());
        orgPO.setParentId(req.getParentIdList().get(req.getParentIdList().size() - 1));
        orgPO.setUpdateTime(new Date());

        boolean success = orgRepository.editOrg(orgPO);
        if (!success)
            return OperationResUtils.fail("编辑组织机构信息失败");

        return OperationResUtils.success();
    }

    @Override
    public Integer getUserOrgId(int userId) {
        return orgRepository.getUserOrgId(userId);
    }
}
